package org.example.minebackend.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.minebackend.pojo.Comment;
import org.example.minebackend.pojo.Invitation;
import org.example.minebackend.pojo.PageBean;
import org.example.minebackend.pojo.Result;
import org.example.minebackend.pojo.dto.CommentDTO;
import org.example.minebackend.pojo.dto.InvitationDetailDTO;
import org.example.minebackend.pojo.dto.InvitationPageDTO;
import org.example.minebackend.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin("*")
@Slf4j
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;

    @PostMapping("/invitation")
    public Result getInvitations(@RequestBody InvitationPageDTO invitationPage) {
        log.info("进行分页的帖子查询");
        PageBean pageBean = communityService.getInvitations(invitationPage);
        return Result.success(pageBean);
    }

    @PostMapping("/upload")
    public Result uploadInvitation(@RequestBody Invitation invitation) {
        log.info("上传帖子");
        int result = communityService.uploadInvitation(invitation);
        if (result == -1) {
            return Result.error("上传失败");
        }
        return Result.success();
    }

    @GetMapping("/invitation/{id}")
    public Result getInvitationById(@PathVariable("id") Integer id) {
        log.info("请求帖子{}", id);
        InvitationDetailDTO result = communityService.getInvitationById(id);
        if (result == null) {return Result.error("error");}
        return Result.success(result);
    }

    @GetMapping("/invitation/comment/{id}")
    public Result getAllCommentByInvitationId(@PathVariable Integer id) {
        List<CommentDTO> result = communityService.getAllCommentByInvitationId(id);
        return Result.success(result);
    }
    @PostMapping("/upload/comment")
    public Result uploadComment(@RequestBody Comment comment) {
        log.info("传入评论为：{}", comment);
        int result = communityService.uploadComment(comment);
        if(result == -1) {return Result.error("error");}
        return Result.success();
    }

    // 实现对帖子的收藏功能
    @GetMapping("/invitation/collection")
    public Result addInvitationOfCollection(@RequestParam Integer userId, @RequestParam Integer invitationId) {
        log.info("插入参数(userId, invitation)：{}, {}", userId, invitationId);
        int n = communityService.addInvitationOfCollection(userId, invitationId);
        if(n == 0) return Result.success("success");
        return Result.error("fail");
    }

    @GetMapping("/invitation/judgecollection")
    public Result judgeCollection(@RequestParam Integer userId, @RequestParam Integer invitationId) {
        log.info("查询参数(userId, invitation)是否存在：{}, {}", userId, invitationId);
        int len = communityService.judgeCollection(userId, invitationId);
        if(len == 1) {
            return Result.success("success");
        }

        return Result.error("fail");
    }

    @DeleteMapping("/invitation/deletecollerction")
    public Result deleteCollection(@RequestParam Integer userId, @RequestParam Integer invitationId) {
        log.info("删除参数(userId, invitation)：{}, {}", userId, invitationId);
        int n = communityService.deleteCollection(userId, invitationId);
        if (n == 1) return Result.success("success");
        return Result.error("fail");
    }
}
